package com.bingxue.edu.management.shop.controller;

import com.bingxue.edu.common.enums.ReceiveTypeEnum;
import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.common.model.Pagination;
import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.framework.common.util.ValidationUtils;
import com.bingxue.edu.framework.security.ApiResource;
import com.bingxue.edu.management.shop.model.entity.CouponTemplate;
import com.bingxue.edu.management.shop.model.query.CouponQuery;
import com.bingxue.edu.management.shop.model.query.CouponTemplateQuery;
import com.bingxue.edu.management.shop.model.query.ParentCouponQuery;
import com.bingxue.edu.management.shop.model.req.CouponDistributeReq;
import com.bingxue.edu.management.shop.model.req.CouponImportReq;
import com.bingxue.edu.management.shop.model.req.CouponTemplateReq;
import com.bingxue.edu.management.shop.model.resp.CouponImportParseResp;
import com.bingxue.edu.management.shop.model.resp.CouponResp;
import com.bingxue.edu.management.shop.model.resp.CouponTemplateResp;
import com.bingxue.edu.management.shop.service.CouponService;
import com.bingxue.edu.management.shop.service.CouponTemplateService;
import com.bingxue.edu.parent.model.resp.ParentCouponDetailResp;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 优惠券绑定
 *
 * @author zq
 * @since 2024/08/22
 */
@Tag(name = "优惠券绑定")
@RestController
@RequestMapping("/shop/coupon")
@RequiredArgsConstructor
public class CouponController {

    private final CouponService couponService;
    private final CouponTemplateService couponTemplateService;

    @Operation(summary = "分页查询优惠信息")
    @ApiResource("shop:coupon:read")
    @GetMapping("/page")
    public Response<Pagination<CouponResp>> getCouponPage(CouponQuery query, @Validated PageQuery pageQuery) {
        return Response.success(Pagination.of(couponService.queryPage(query,pageQuery)));
    }

    @Operation(summary = "查询优惠券列表")
//    @ApiResource("shop:coupon:read")
    @GetMapping("/list-by-parent")
    public Response<List<ParentCouponDetailResp>> getParentCouponList(@Validated ParentCouponQuery query) {
        return Response.success(couponService.getParentCouponList(query));
    }

    @Operation(summary = "发放优惠券")
    @ApiResource("shop:coupon:distribute")
    @PostMapping("/distribute")
    public Response<Void> distributeCoupon(@RequestBody @Validated CouponDistributeReq req) {
        req.setReceiveType(ReceiveTypeEnum.BACKEND);
        couponService.distributeCoupon(req);
        return Response.success();
    }

    @Operation(summary = "删除优惠券")
    @ApiResource("shop:coupon:write")
    @PostMapping("/delete/{id}")
    public Response<Void> deleteCoupon(@PathVariable String id) {
        couponService.deleteById(id);
        return Response.success();
    }

    @Operation(summary = "撤销优惠券")
    @ApiResource("shop:coupon:distribute")
    @PostMapping("/cancel/{couponId}")
    public Response<Void> cancelCoupon(@PathVariable String couponId) {
        couponService.cancelCoupon(couponId);
        return Response.success();
    }

    @Operation(summary = "创建优惠券模板")
    @ApiResource("shop:coupon:write")
    @PostMapping("/template/create")
    public Response<Void> createTemplate(@RequestBody @Validated CouponTemplateReq req) {
        couponTemplateService.create(req);
        return Response.success();
    }

    @Operation(summary = "分页查询优惠卷模板")
    @ApiResource("shop:coupon:read")
    @GetMapping("/template/page")
    public Response<Pagination<CouponTemplateResp>> getTemplatePage(CouponTemplateQuery query, @Validated PageQuery pageQuery) {
        Page<CouponTemplateResp> page = couponTemplateService.queryPage(query, pageQuery);
        return Response.success(Pagination.of(page));
    }

    @Operation(summary = "查询优惠卷模板详情")
    @ApiResource("shop:coupon:read")
    @GetMapping("/template/detail/{id}")
    public Response<CouponTemplate> getTemplateDetail(@PathVariable String id) {
        return Response.success(couponTemplateService.getDetailById(id));
    }

    @Operation(summary="更新优惠券模板")
    @ApiResource("shop:coupon:write")
    @PostMapping("/template/update/{id}")
    public Response<Void> updateTemplate(@RequestBody @Validated CouponTemplateReq req, @PathVariable String id) {
        CouponTemplate couponTemplate = couponTemplateService.copyById(req, id);
        if (req.getSingleClassIdLimit() != null) {
            couponTemplate.setClassIdLimit(List.of(req.getSingleClassIdLimit()));
        }
        couponTemplateService.updateById(couponTemplate);
        return Response.success();
    }

    @Operation(summary="删除优惠券模板")
    @ApiResource("shop:coupon:write")
    @PostMapping("/template/delete/{id}")
    public Response<Void> deleteTemplate(@PathVariable String id) {
        couponTemplateService.deleteById(id);
        return Response.success();
    }


    @Operation(summary = "下载优惠卷导入模板", description = "下载优惠卷导入模板")
    @ApiResource("shop:coupon:distribute")
    @GetMapping(value = "downloadImportCouponTemplate", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void downloadImportCouponTemplate(HttpServletResponse response) throws IOException {
        couponService.downloadImportCouponTemplate(response);
    }


    @Operation(summary = "解析优惠卷导入数据", description = "解析优惠卷导入数据")
    @ApiResource("shop:coupon:distribute")
    @PostMapping(value = "parseImportCoupon")
    public Response<CouponImportParseResp> parseImportCoupon(@NotNull(message = "文件不能为空") MultipartFile file) {
        ValidationUtils.throwIf(file::isEmpty, "文件不能为空");
        return Response.success(couponService.parseImportCoupon(file));
    }

    @Operation(summary = "导入优惠卷", description = "导入优惠卷")
    @ApiResource("shop:coupon:distribute")
    @PostMapping(value = "import")
    public Response<Void> importCoupon(@Validated @RequestBody CouponImportReq req) {
        couponService.importCoupon(req);
        return Response.success();
    }

}
